/*
* Filename Interface.cpp
*/

#include "stdafx.h"
#include "Interface.h"
#include <crtdbg.h>

namespace DeepZoomImage
{

	IDeepZoomImage::IDeepZoomImage()
		: m_pDZITS(NULL)
		, m_pMSI(NULL)
		, m_pSurfaceInfo(NULL)
		, m_pInterface(NULL)
		, m_bInitFinish(false)
		, m_bIsScale(false)
		, m_bIsPanning(false)
		, m_scale(1.0)
		, m_ratio(1.2) 
		, m_vp_w(1.0)
		, m_actualWidth(1024)
		, m_pos(Point(0.0, 0.0))
		, m_mousePos(Point(0.0, 0.0))
		, m_windowWidth(0)
		, m_windowHeight(0)
	{

	}

	IDeepZoomImage::~IDeepZoomImage()
	{		
		Unload();
	}

	bool IDeepZoomImage::Initialize()
	{
		if (NULL == m_pDZITS)
		{
			m_pDZITS = new DeepZoomImageTileSource();
			m_pDZITS->loadRes();
		}

		if (NULL == m_pMSI && NULL != m_pDZITS)
		{
			m_pMSI = new MultiScaleImage();

			// 	m_pMSI->SetSource( m_pDZITS );	
			// 	m_pMSI->HandleDzParsed();
		}

		if (NULL == m_pSurfaceInfo)
		{
			m_pSurfaceInfo = new CairoSurfaceInfo();
		}

		if (NULL != m_pDZITS && NULL != m_pMSI)
		{
			m_bInitFinish = true;
		}
		else
		{
			m_bInitFinish = false;
		}
		return m_bInitFinish;
	}

	void IDeepZoomImage::Unload()
	{
		if (NULL != m_pDZITS)
		{
			m_pDZITS->unloadRes();
			delete m_pDZITS;
			m_pDZITS = NULL;
		}
		if (NULL != m_pMSI)
		{
			delete m_pMSI;
			m_pMSI = NULL;
		}
		if (NULL != m_pSurfaceInfo)
		{
			delete m_pSurfaceInfo;
			m_pSurfaceInfo = NULL;
		}
	}

	void IDeepZoomImage::UpdataRender()
	{
		if (NULL != m_pMSI)
		{
			m_pMSI->UpdateStatus();
			m_pMSI->Render();
			if(NULL != m_pInterface)
			{
				m_pInterface->UpdataRender(m_pSurfaceInfo->mSurfaceWidth,m_pSurfaceInfo->mSurfaceHeight,m_pSurfaceInfo->mSurfaceData);
			}
		}
	}

	void IDeepZoomImage::SetCairoSurfaceInfo(int width,int height,unsigned char* buffer)
	{
		if (NULL != m_pMSI && NULL != m_pSurfaceInfo)
		{
			m_pSurfaceInfo->mSurfaceWidth = width;
			m_pSurfaceInfo->mSurfaceHeight = height;
			m_pSurfaceInfo->mSurfaceData  = buffer;

			m_pMSI->SetCairoSurface(m_pSurfaceInfo->mSurfaceWidth,m_pSurfaceInfo->mSurfaceHeight,m_pSurfaceInfo->mSurfaceData);

			m_pMSI->SetSource( m_pDZITS );	
			m_pMSI->HandleDzParsed();
		}
	}
	void IDeepZoomImage::SetWindowSize(int windowWidth,int windowHeight)
	{
		m_windowWidth = windowWidth;
		m_windowHeight = windowHeight;
	}

	void IDeepZoomImage::SetImageMove(double posX,double posY)
	{
		if (m_pMSI)
		{
			m_pos = Point(posX,posY);
			m_pMSI->OnPropertyChanged( ViewportOriginProperty, &m_pos);
		}
	}

	void IDeepZoomImage::SetImageZoom(double zDelta,double posX,double posY)
	{
		if (m_pMSI)
		{
			Point logicalPt = m_pMSI->ElementToLogicalPoint( Point( posX, posY ) );
			m_pMSI->ZoomAboutLogicalPoint( zDelta, logicalPt.x, logicalPt.y );
		}
	}

	Point* IDeepZoomImage::GetAnimatedViewportOrigin()
	{
		if (m_pMSI)
		{
		    return m_pMSI->GetAnimatedViewportOrigin();
		}
	}

	double IDeepZoomImage::Getviewportwidth()
	{
		if (m_pMSI)
		{
			return m_pMSI->GetAnimatedViewportWidth();
		}
	}
	double IDeepZoomImage::GetActualWidth()
	{
		if (m_pMSI)
		{
			return m_pMSI->GetActualWidth();
		}
	}

	/*
	void IDeepZoomImage::SetImageMove(int moveDeltaX,int moveDeltaY)
	{
		if ( m_pMSI )
		{
			m_vp_w = m_pMSI->GetAnimatedViewportWidth();
			m_actualWidth = m_pMSI->GetActualWidth();
		}
		else
			return;	

		m_pos.x -= (double)moveDeltaX/ m_actualWidth * m_vp_w;
		m_pos.y -= (double)moveDeltaY/ m_actualWidth * m_vp_w;
		m_bIsPanning = true;

		MouseMoveEvent();
	}

	void IDeepZoomImage::MouseMoveEvent()
	{
		if ( m_bIsPanning )
		{
			m_pMSI->OnPropertyChanged( ViewportOriginProperty, &m_pos);
		}
	}

	void IDeepZoomImage::SetImageZoom(int zDelta,int posX,int posY)
	{
		if ( m_pMSI )
		{
			m_vp_w = m_pMSI->GetAnimatedViewportWidth();
			m_actualWidth = m_pMSI->GetActualWidth();
		}
		else
			return;

		if ( abs( zDelta ) > 0 )
		{
			if ( zDelta > 0 )
			{
				m_scale = 1.0/m_ratio;
			}
			else
			{
				m_scale = m_ratio;
			}			
			m_bIsScale = true;
		}

		MouseWheelEvent(posX,posY);
	}

	void IDeepZoomImage::MouseWheelEvent(int posX,int posY)
	{
		if ( m_bIsScale )
		{
			m_mousePosX = CLAMP( posX, 0, 1024 );
			m_mousePosY = CLAMP( posY, 0, 768);

			Point logicalPt = m_pMSI->ElementToLogicalPoint( Point( m_mousePosX, m_mousePosY ) );

			if ( m_scale >= 1.0 && m_vp_w >= 0.02 || m_scale < 1.0 && m_vp_w <= 5.0 )
			{
				m_pMSI->ZoomAboutLogicalPoint( m_scale, logicalPt.x, logicalPt.y );
				Point *animVpOrigin = m_pMSI->GetAnimatedViewportOrigin();
				m_pos.x = animVpOrigin->x;
				m_pos.y = animVpOrigin->y;
			}
		}
	}
	*/
}